﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="check_by_month.aspx.cs" Inherits="SQT.tracnghiem.Ajax.ASP.Accountant.check_by_month" %>

<%@ Import Namespace="DataAccess" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <thead>
        <tr class="w3-teal">
            <th>Mã NV</th>
            <th>Họ tên</th>
            <th>Ngày làm</th>
            <th>Chi tiết giờ làm</th>
            <th>Tổng số giờ</th>
        </tr>
    </thead>

    <%
        int month = int.Parse(Request.QueryString["date_month"]);
        int year = int.Parse(Request.QueryString["date_year"]);
        int staff = int.Parse(Request.QueryString["staff"]);

        if (staff == 0)
        {
            for (int i = 0; i <= 31; i += 7)
            {
    %>
    <tr>
        <th colspan="5" class="w3-center w3-text-teal w3-large">Tuần <%=i/7+1 %></th>
    </tr>
    <%
                foreach (TblCheckManager chk in new TblCheckManagerController().FetchAll().Where(x => DateTime.Parse(x.CheckIn.ToString()).Month == month && DateTime.Parse(x.CheckIn.ToString()).Year == year && DateTime.Parse(x.CheckIn.ToString()).Day > i && DateTime.Parse(x.CheckIn.ToString()).Day <= i + 7 && x.Accept == true))
                {
                    var get_admin = new TblManagerController().FetchAll().Where(x => x.ManagerId == chk.ManagerId).Single();
                    string checkout = "";
                    float hour = 0, minute = 0;
                    if (chk.CheckOut != null)
                    {
                        checkout = DateTime.Parse(chk.CheckOut.ToString()).Hour.ToString() + ":" + DateTime.Parse(chk.CheckOut.ToString()).Minute.ToString();
                        if (DateTime.Parse(chk.CheckOut.ToString()).Hour >= DateTime.Parse(chk.CheckIn.ToString()).Hour)
                        {
                            hour = DateTime.Parse(chk.CheckOut.ToString()).Hour - DateTime.Parse(chk.CheckIn.ToString()).Hour;
                        }
                        else
                        {
                            hour = 24 - DateTime.Parse(chk.CheckIn.ToString()).Hour + DateTime.Parse(chk.CheckOut.ToString()).Hour;
                        }

                        if (DateTime.Parse(chk.CheckIn.ToString()).Minute <= DateTime.Parse(chk.CheckOut.ToString()).Minute)
                        {
                            minute = DateTime.Parse(chk.CheckOut.ToString()).Minute - DateTime.Parse(chk.CheckIn.ToString()).Minute;
                        }
                        else
                        {
                            minute = 60 - DateTime.Parse(chk.CheckIn.ToString()).Minute + DateTime.Parse(chk.CheckOut.ToString()).Minute;
                            hour--;
                        }
                    }
                              
    %>
    <tr>
        <td><%=get_admin.ManagerId%></td>
        <td><%=get_admin.FullName%></td>
        <td><%=DateTime.Parse(chk.CheckIn.ToString()).Day%>/<%=DateTime.Parse(chk.CheckIn.ToString()).Month%>/<%=DateTime.Parse(chk.CheckIn.ToString()).Year%></td>
        <td><%=DateTime.Parse(chk.CheckIn.ToString()).Hour%>:<%=DateTime.Parse(chk.CheckIn.ToString()).Minute%> - <%=checkout%></td>
        <td><%=hour%> giờ <%=minute%> phút</td>
    </tr>
    <%
                }
            }
        }
        else
        {
            for (int i = 0; i <= 31; i += 7)
            {
    %>
    <tr>
        <th colspan="5" class="w3-center w3-text-teal w3-large">Tuần <%=i/7+1 %></th>
    </tr>
    <%
                foreach (TblCheckManager chk in new TblCheckManagerController().FetchAll().Where(x => DateTime.Parse(x.CheckIn.ToString()).Month == month && DateTime.Parse(x.CheckIn.ToString()).Year == year && x.ManagerId == staff && DateTime.Parse(x.CheckIn.ToString()).Day > i && DateTime.Parse(x.CheckIn.ToString()).Day <= i + 7 && x.Accept == true))
                {
                    var get_admin = new TblManagerController().FetchAll().Where(x => x.ManagerId == chk.ManagerId).Single();
                    string checkout = "";
                    float hour = 0, minute = 0;
                    if (chk.CheckOut != null)
                    {
                        checkout = DateTime.Parse(chk.CheckOut.ToString()).Hour.ToString() + ":" + DateTime.Parse(chk.CheckOut.ToString()).Minute.ToString();
                        if (DateTime.Parse(chk.CheckOut.ToString()).Hour >= DateTime.Parse(chk.CheckIn.ToString()).Hour)
                        {
                            hour = DateTime.Parse(chk.CheckOut.ToString()).Hour - DateTime.Parse(chk.CheckIn.ToString()).Hour;
                        }
                        else
                        {
                            hour = 24 - DateTime.Parse(chk.CheckIn.ToString()).Hour + DateTime.Parse(chk.CheckOut.ToString()).Hour;
                        }

                        if (DateTime.Parse(chk.CheckIn.ToString()).Minute <= DateTime.Parse(chk.CheckOut.ToString()).Minute)
                        {
                            minute = DateTime.Parse(chk.CheckOut.ToString()).Minute - DateTime.Parse(chk.CheckIn.ToString()).Minute;
                        }
                        else
                        {
                            minute = 60 - DateTime.Parse(chk.CheckIn.ToString()).Minute + DateTime.Parse(chk.CheckOut.ToString()).Minute;
                            hour--;
                        }
                    }
                              
    %>
    <tr>
        <td><%=get_admin.ManagerId%></td>
        <td><%=get_admin.FullName%></td>
        <td><%=DateTime.Parse(chk.CheckIn.ToString()).Day%>/<%=DateTime.Parse(chk.CheckIn.ToString()).Month%>/<%=DateTime.Parse(chk.CheckIn.ToString()).Year%></td>
        <td><%=DateTime.Parse(chk.CheckIn.ToString()).Hour%>:<%=DateTime.Parse(chk.CheckIn.ToString()).Minute%> - <%=checkout%></td>
        <td><%=hour%> giờ <%=minute%> phút</td>
    </tr>
    <%}
            }%>

    <%} %>
</body>
</html>
